<template>
	<view style="margin-top:400rpx;">
		<view style="width: 100%;text-align: center;">当前地址:{{area}}</view>
		<view
			style="
		margin-left:35%;margin-top:60rpx;
		width:250rpx;height:250rpx;line-height:250rpx;color:white;background-color: #60a5fa;border-radius: 150px;text-align: center;"
			@tap="getAuthorize">
			打卡
		</view>
	</view>
</template>

<script>
	import QQMapWx from '@/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js'
	// 2、实例化API核心类
	const qqmapSdk = new QQMapWx({
		key: 'S3OBZ-YGPW2-VSFUH-CGEKB-RUVPS-PMFKF'
	});
	import cityPicker from '@/uni_modules/piaoyi-cityPicker/components/piaoyi-cityPicker/piaoyi-cityPicker';
	import config from "@/config/config.js";
	import xcUniappUtils from '@/library/xcore/js/XcUniappUtils.js';
	import xcViewUtils from '@/library/xcore/js/XcViewUtils.js';
	import xcHttpUtils from '@/library/xcore/js/XcHttpUtils.js';
	import xcCloginUtils from '@/library/xcore/js/XcCloginUtils.js';
	import {
		xsStringUtils,
		xsDateUtils,
		xsArrayUtils
	} from 'xstack-for-javascript';

	export default {
		data() {
			return {
				area: '用户未授权，获取当前位置失败'
			}
		},
		onShow() {
			this.getLocation2('gcj02')
		},
		methods: {
			getAuthorize() {
				console.log(44444)
				uni.authorize({
					scope: 'scope.userLocation',
					success: (res) => {
						this.getLocation('gcj02');
					},
					fail: (err) => {
						uni.showModal({
							content: '需要授权位置信息',
							confirmText: '确认授权'
						}).then(res => {
							if (res['confirm']) {
								uni.openSetting({
									success: res => {
										if (res.authSetting['scope.userLocation']) {
											uni.showToast({
												title: '授权成功',
												icon: 'none'
											})
										} else {
											uni.showToast({
												title: '授权失败',
												icon: 'none'
											})
										}
										this.getLocation('gcj02')
									}
								})
							}
							if (res['cancel']) {
								// 取消授权
								this.getLocation('gcj02')
							}
						})
					}
				})
			},
			getLocation(type) {
				var that = this
				uni.getLocation({
					type: type,
					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
					success: (res) => {

						const {
							latitude,
							longitude
						} = res;

						qqmapSdk.reverseGeocoder({
							location: latitude ? `${latitude},${longitude }` : '',
							sig: 'S3OBZ-YGPW2-VSFUH-CGEKB-RUVPS-PMFKF', // 这个sig就是上面要准备的第二项SK
							success: (val) => {
								console.log('111111', val.result.ad_info.city)
								that.area = val.result.address

								uni.showModal({
									title: '提示',
									content: '当前地址:' + that.area,
									showCancel: true, // 是否显示取消按钮，默认为 true
									cancelText: '取消打卡',
									confirmText: '确定打卡',
									success: (res2) => {
										if (res2.confirm) {

											xcCloginUtils.getUserinfo(function(userinfo) {

												console.log(444, userinfo.id)
												uni.request({
													url: 'https://shanghaihuxi.cn/api/index/daka',
													data: {
														'user_id': userinfo.id,
														'area': that.area,
														'city': val.result.ad_info.city
													},
													header: {
														'Content-Type': 'application/json' // 请求头，可以根据接口要求修改
													},
													success: (res) => {
														uni.showToast({
															title: '打卡成功',
															icon: 'success',
															duration: 2000,
															success: () => {
																setTimeout
																	(() => {
																			// 这里将 'your-page-path' 替换为你要跳转的页面路径
																			uni.navigateTo({
																				url: '/pages/xclogin/me'
																			});
																		},
																		2000
																		);
															}
														});
													}
												});
											});



										} else if (res.cancel) {

										}
									}
								});

							},
							fail: (err) => {
								console.log('获取城市失败')
							}
						})
					},
					fail: (err) => {

						uni.stopPullDownRefresh();
						if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' || err
							.errMsg === 'getLocation:fail system permission denied') {
							uni.showModal({
								content: '请开启手机定位服务',
								showCancel: false
							})
						} else if (err.errMsg === 'getLocation:fail:system permission denied') {
							uni.showModal({
								content: '请给微信开启定位权限',
								showCancel: false
							})
						}
					}
				})

			},

			getLocation2(type) {
				var that = this
				uni.getLocation({
					type: type,
					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
					success: (res) => {

						const {
							latitude,
							longitude
						} = res;

						qqmapSdk.reverseGeocoder({
							location: latitude ? `${latitude},${longitude }` : '',
							sig: 'S3OBZ-YGPW2-VSFUH-CGEKB-RUVPS-PMFKF', // 这个sig就是上面要准备的第二项SK
							success: (val) => {
								console.log('4567', val.result.address)
								that.area = val.result.address


							},
							fail: (err) => {
								console.log('获取城市失败')
							}
						})
					},
					fail: (err) => {
						console.log(555,err)
						uni.stopPullDownRefresh();
						if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' || err
							.errMsg === 'getLocation:fail system permission denied') {
							uni.showModal({
								content: '请开启手机定位服务',
								showCancel: false
							})
						} else if (err.errMsg === 'getLocation:fail:system permission denied') {
							uni.showModal({
								content: '请给微信开启定位权限',
								showCancel: false
							})
						}
					}
				})
			}

		},
		watch: {

		}
	}
</script>

<style lang="scss" scoped>

</style>
